// 等待DOM加载完成
document.addEventListener('DOMContentLoaded', function() {
    // 获取所有密码输入框和切换按钮
    const oldPasswordInput = document.getElementById('old_password');
    const newPasswordInput = document.getElementById('new_password');
    const confirmPasswordInput = document.getElementById('confirm_password');
    
    const toggleOldPasswordBtn = document.getElementById('toggle-old-password');
    const toggleNewPasswordBtn = document.getElementById('toggle-new-password');
    const toggleConfirmPasswordBtn = document.getElementById('toggle-confirm-password');
    
    // 为当前密码添加可见性切换功能
    toggleOldPasswordBtn.addEventListener('click', function() {
        togglePasswordVisibility(oldPasswordInput, toggleOldPasswordBtn);
    });
    
    // 为新密码添加可见性切换功能
    toggleNewPasswordBtn.addEventListener('click', function() {
        togglePasswordVisibility(newPasswordInput, toggleNewPasswordBtn);
    });
    
    // 为确认新密码添加可见性切换功能
    toggleConfirmPasswordBtn.addEventListener('click', function() {
        togglePasswordVisibility(confirmPasswordInput, toggleConfirmPasswordBtn);
    });
    
    // 表单提交时确保所有密码字段是密码类型
    const passwordForm = document.querySelector('form');
    if (passwordForm) {
        passwordForm.addEventListener('submit', function() {
            // 重置所有密码输入框为密码类型
            resetPasswordFields();
        });
    }
    
    /**
     * 切换密码输入框的可见性
     * @param {HTMLElement} passwordInput - 密码输入框元素
     * @param {HTMLElement} toggleButton - 切换按钮元素
     */
    function togglePasswordVisibility(passwordInput, toggleButton) {
        const icon = toggleButton.querySelector('i');
        const type = passwordInput.getAttribute('type') === 'password' ? 'text' : 'password';
        passwordInput.setAttribute('type', type);
        
        // 切换图标
        if (type === 'password') {
            icon.classList.remove('fa-eye');
            icon.classList.add('fa-eye-slash');
        } else {
            icon.classList.remove('fa-eye-slash');
            icon.classList.add('fa-eye');
        }
    }
    
    /**
     * 重置所有密码字段为密码类型
     */
    function resetPasswordFields() {
        // 重置当前密码输入框
        oldPasswordInput.setAttribute('type', 'password');
        const oldPasswordIcon = toggleOldPasswordBtn.querySelector('i');
        oldPasswordIcon.classList.remove('fa-eye');
        oldPasswordIcon.classList.add('fa-eye-slash');
        
        // 重置新密码输入框
        newPasswordInput.setAttribute('type', 'password');
        const newPasswordIcon = toggleNewPasswordBtn.querySelector('i');
        newPasswordIcon.classList.remove('fa-eye');
        newPasswordIcon.classList.add('fa-eye-slash');
        
        // 重置确认新密码输入框
        confirmPasswordInput.setAttribute('type', 'password');
        const confirmPasswordIcon = toggleConfirmPasswordBtn.querySelector('i');
        confirmPasswordIcon.classList.remove('fa-eye');
        confirmPasswordIcon.classList.add('fa-eye-slash');
    }
});